<?php

class Member_model extends CI_Model
{

    private $_table = 'sm_member';

    public function __construct()
    {
        parent::__construct();
    }

    function getAll($conditions = array(), $fields = '', $like = array(),
            $limit = array(), $orderby = array(), $conditionFilter = '', $or_where = array())
    {
        //Check For Conditions

        if (is_array($conditions) and count($conditions) > 0)
            $this->db->where($conditions);
        //$this->db->where(array("active"=>1));
        if(!empty($or_where)){
            $this->db->or_where($or_where);
        }

        //Check For Conditions Filter
        if ($conditionFilter != '')
            $this->db->where($conditionFilter);
        //Check For like statement
        if (is_array($like) and count($like) > 0)
            $this->db->like($like);
        //Check For Limit
        if (is_array($limit)) {
            if (count($limit) == 1)
                $this->db->limit($limit [0]);
            else if (count($limit) == 2)
                $this->db->limit($limit [0], $limit [1]);
        }
        //Check for Order by
        if (is_array($orderby) and count($orderby) > 0) {
            $this->db->order_by($orderby [0], $orderby [1]);
        } else {
            $this->db->order_by('c.id', 'ASC');
        }

        $this->db->from("{$this->_table} c");
        if ($fields != '') {
            $this->db->select($fields);
        } else {
            $this->db->select("c.*");
        }

        $result = $this->db->get();

        return $result;
    }

    public function getAllType($conditions = array(), $fields = '', $like = array(),
            $limit = array(), $orderby = array(), $conditionFilter = array(), $groupby = '')
    {
        //Check For Conditions
        if (is_array($conditions) and count($conditions) > 0)
            $this->db->where($conditions);

        //Check For Conditions Filter
        if (count($conditionFilter) > 0)
            $this->db->or_like($conditionFilter);

        //Check For like statement
        if (is_array($like) and count($like) > 0)
            $this->db->like($like);

        //Check For Limit
        if (is_array($limit)) {
            if (count($limit) == 1)
                $this->db->limit($limit [0]);
            else if (count($limit) == 2)
                $this->db->limit($limit [0], $limit [1]);
        }

        //Check for Order by
        if (is_array($orderby) and count($orderby) > 0) {
            $this->db->order_by($orderby [0], $orderby [1]);
        } else {
            $this->db->order_by('c.id', 'ASC');
        }

        // Check Group by
        if ($groupby != '') {
            $this->db->group_by($groupby);
        }

        $this->db->from("{$this->_table} c");
        if ($fields != '') {
            $this->db->select($fields);
        } else {
            $this->db->select("c.*");
        }

        $result = $this->db->get();
        return $result;
    }

    public function createMember($insertData = array())
    {
        $this->db->insert($this->_table, $insertData);
        $last = $this->db->insert_id();
        return $last;
    }

    function updateMember($updateKey = array(), $updateData = array(), $typeWhere = '')
    {
        if ($typeWhere == 'IN') {
            $key = array_keys($updateKey);
            $this->db->where_in($key[0], $updateKey[$key[0]]);
        } else
            $this->db->where($updateKey);

        $result = $this->db->update($this->_table, $updateData);
        return $result;
    }

    public function getMemberCuid($cuid)
    {
        $this->db->from($this->_table);
        $this->db->select('id');
        $this->db->where(array('member_name' => $cuid));
        $rs = $this->db->get();
        if($rs->num_rows()>0){
            return $rs->row();
        }else{
            return false;
        }

    }

    public function disableUserlist($userlist){
        $userlist = "'".implode("','", $userlist)."'";
        $query = "update sm_member set active = 0 where member_name in ({$userlist})";
        $this->db->query($query);
    }
}
